1617C - Paprika and Permutation - CodeForces Solution


binary search greedy math sortings *1300

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#ifndef ONLINE_JUDGE
#include <algodebug>
#else
#define debug(...)
#endif
#define ll long long
#define mod (ll)(1e9+7)
#define tc ll t;cin>>t;while(t--)
void solve(){
  int n,ans=0;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
    cin>>a[i];
  }
  sort(a,a+n);
  vector<bool>vis(n+1,0);
  vector<int> nums;
  for(int i=0;i<n;i++){
    if(a[i]>=1&&a[i]<=n){
    if(vis[a[i]]){
     nums.push_back(a[i]);
    }
    else
    vis[a[i]]=true;
  }else{
    nums.push_back(a[i]);
  }
  }
  int j=0;
  for(int i=1;i<=n;i++){
    if(!vis[i]){
      if(nums[j++]<=2*i){
        cout<<-1<<endl;
        return;
      }
      ans++;
    }
  }
  cout<<ans<<endl;
}
int main() {
  fastio();
  tc
  solve();
}


Comments

Submit
0 Comments
More Questions

1433C - Dominant Piranha
633A - Ebony and Ivory
1196A - Three Piles of Candies
299A - Ksusha and Array
448B - Suffix Structures
1092B - Teams Forming
1166C - A Tale of Two Lands
544B - Sea and Islands
152B - Steps
1174D - Ehab and the Expected XOR Problem
1511A - Review Site
1316A - Grade Allocation
838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement
903A - Hungry Student Problem
52B - Right Triangles
1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer